package com.example.flower_shop.repository;

import com.example.flower_shop.entity.Review;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;

@Repository
public interface ReviewRepository extends JpaRepository<Review, Long> {
    Page<Review> findByProductId(Long productId, Pageable pageable);

    Page<Review> findByUserIdOrderByCreateTimeDesc(Long userId, Pageable pageable);

    boolean existsByUserIdAndProductIdAndOrderNo(Long userId, Long productId, String orderNo);

    Long countByProductId(Long productId);

    @Query("SELECT COALESCE(AVG(r.rating), 0.0) FROM Review r WHERE r.product.id = ?1")
    Double getAverageRatingByProductId(Long productId);

    // 以下是商家管理评论需要的方法
    Page<Review> findByContentContaining(String keyword, Pageable pageable);

    Page<Review> findByStatus(Integer status, Pageable pageable);

    Page<Review> findByCreateTimeBetween(LocalDateTime startTime, LocalDateTime endTime, Pageable pageable);
}